package com.example.function.integration.function.integration.utils;

import com.alibaba.fastjson2.util.DateUtils;
import com.example.function.integration.config.CommonBeanConfig;
import com.example.function.integration.exception.BizException;
import com.example.function.integration.function.integration.FunctionIntegrationApplicationTests;
import com.example.function.integration.utils.CryptoUtils;
import com.example.function.integration.utils.ExcelUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

import static com.example.function.integration.constant.CommonConstant.XLSX;

/**
 * @Description：工具测试类
 * @Author： fengcheng
 * @Date： 2024/10/9 11:38
 */
@RunWith(SpringRunner.class)
public class ToolClassTest extends FunctionIntegrationApplicationTests {

    @Resource
    private CommonBeanConfig commonBeanConfig;

    @Test
    public void testUtils() {
        // 测试加密工具类的方法
        testCryptoUtils();

        // 测试获取最小数字
        testGetMinNum();

        // 测试Excel工具类的方法
        testExcelUtils();
    }

    public void testCryptoUtils() {
        // 身份证号码加密，解密
        String cardNo = "5109874667263864";
        System.out.println("需要加密的身份证号码：" + cardNo);
        System.out.println("身份证号码加密：" + CryptoUtils.encryptMode(cardNo));
        System.out.println("身份证号码解密：" + CryptoUtils.decryptMode(CryptoUtils.encryptMode(cardNo)));
    }

    public void testGetMinNum() {
        // 计算出最小的数字
        String minStr = "2310150,2305616,2304330,2300773,2281190,2263879,2255498,2253860,2263091,2249526";
        String[] splitStrArra = minStr.split(",");
        System.out.println("id总数：【" + splitStrArra.length + "】");
        int min = Integer.MAX_VALUE;
        for (String value : splitStrArra) {
            int number = Integer.parseInt(value);
            if (number < min) {
                min = number;
            }
        }
        System.out.println("计算出最小的数字：【" + min + "】");
    }

    public void testExcelUtils() {
        System.out.println("开始处理导出文件数据并生成文件到本地,开始执行时间：" + DateUtils.toString(new Date()));
        try {
            Map<String, List<List<Object>>> sheets = getExcelSheetDataMap();
            String fileName = "用户信息导出文件";
            ExcelUtils.exportManySheet(fileName + XLSX, false, commonBeanConfig.getThreadPollObj(), sheets, null);
            System.out.println("导出文件数据，执行完成,执行结束时间：" + DateUtils.toString(new Date()));
        } catch (BizException e) {
            System.out.println("导出文件数据发生异常，异常信息：" + e.getMessage() + "\n" + e);
            throw new BizException("导出文件数据发生异常");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description：获取Excel Sheet Map数据
     * @Author：fengcheng
     * @Date：2024/10/10 09:16
     * @Return：java.util.Map<java.lang.String,java.util.List<java.util.List<java.lang.Object>>>
     */
    private static Map<String, List<List<Object>>> getExcelSheetDataMap() {
        // 第 1 个 Sheet 页
        List<List<Object>> sheet1 = new ArrayList<>();
        List<Object> sheetHead = Arrays.asList("姓名", "年龄", "性别", "电话", "城市", "邮编", "性别", "地址", "电子邮箱", "详细地址");
        sheet1.add(sheetHead);
        getUserSheelData(sheet1, "1");

        // 第 2 个 Sheet 页
        List<List<Object>> sheet2 = new ArrayList<>();
        sheet2.add(sheetHead);
        getUserSheelData(sheet2, "2");

        // 第 3 个 Sheet 页
        List<List<Object>> sheet3 = new ArrayList<>();
        sheet3.add(sheetHead);
        getUserSheelData(sheet3, "3");

        // 第 3 个 Sheet 页
        List<List<Object>> sheet4 = new ArrayList<>();
        sheet4.add(sheetHead);
        getUserSheelData(sheet4, "4");

        // 第 3 个 Sheet 页
        List<List<Object>> sheet5 = new ArrayList<>();
        sheet5.add(sheetHead);
        getUserSheelData(sheet5, "5");

        // 将两个 Sheet 页添加到集合中
        Map<String, List<List<Object>>> sheets = new LinkedHashMap<>();
        sheets.put("第1批用户", sheet1);
        sheets.put("第2批用户", sheet2);
        sheets.put("第3批用户", sheet3);
        sheets.put("第4批用户", sheet4);
        sheets.put("第5批用户", sheet5);
        return sheets;
    }

    /**
     * @Description：初始化到处Excel数据
     * @Author：fengcheng
     * @Date：2024/10/9 13:46
     * @Param：sheetData
     * @ParamsType：[java.util.List<java.util.List<java.lang.Object>>]
     * @Return：void
     */
    private static void getUserSheelData(List<List<Object>> sheetData, String tag) {
        for (int i = 0; i < 100000; i++) {
            int constant = i + 1;
            List<Object> rowData = new ArrayList<>();
            rowData.add(tag + "王一" + constant);
            rowData.add(tag + "18" + constant);
            rowData.add(tag + "男" + constant);
            rowData.add(tag + "150" + constant);
            rowData.add(tag + "成都" + constant);
            rowData.add(tag + "5555" + constant);
            rowData.add(tag + "本科" + constant);
            rowData.add(tag + "南苑" + constant);
            rowData.add(tag + "3@" + constant);
            rowData.add(tag + "5楼" + constant);
            sheetData.add(rowData);
        }
    }
}
